/**
 * webbx 主页面的js逻辑
 */

function isOtherUserLogin()
{    
	window.intervalID = setInterval("judgeIsOtherUserLogin();", 10*1000);
}

/**
 * 每隔一段时间连接server，取得用户状态
 */
function judgeIsOtherUserLogin()
{	
	if("production" != window.debugMode)
		return;
	var cmd = new CommonCommand('judgeIsOtherUserLogin');
	cmd.setAsync(true);
	cmd.setReturnFunc(returnFucForJuage);
	cmd.exec();			
   	//processCommonLogic(null, "judgeIsOtherUserLogin", returnFucForJuage, null, true, null, null);
   	function returnFucForJuage(result, returnArgs, success)
   	{
   		if(success){
   			if(result == 'goout'){
   				clearInterval(window.intervalID);//清除周期链接server
   				showMessageDialog("${ml:trans('yer_main001')}", gotoLoginPageFuc);
   			}else if(result == 'exist'){
   				clearInterval(window.intervalID);
   				showMessageDialog("${ml:trans('yer_main002')}", gotoLoginPageFuc);
   			}else if(result == 'session_out'){
   				clearInterval(window.intervalID);
   				showMessageDialog("${ml:trans('yer_main003')}", gotoLoginPageFuc);
   			}
   		}
   		else{
	   		showMessageDialog("${ml:trans('yer_main004')}",gotoLoginPageFuc);
   		}
   	}
}

/**
 * 关闭整个浏览器时调用该方法
 */
function closeBrowser()
{	
	// 注销用户
	var cmd = new CommonCommand('userLogOut');
	cmd.setAsync(false);
	cmd.exec();
	//processCommonLogic(null, "userLogOut", null, null, false, null, null);
}

/**
 * @priate
 */
function gotoLoginPageFuc()
{ 
	window.location.href = window.corePath + "/login/login.jsp?rand=" + (Math.random()*10000).toString().substring(0, 4);
//	if(IS_IE6)
//			win = window.open( window.corePath + "/login/login.jsp?rand=" + (Math.random()*10000).toString().substring(0, 4), null, "toolbar=no,menubar=no,scrollbar=yes,resizable=yes,location=no,status=no,fullscreen=no");
//	else
//		win = window.open( window.corePath + "/login/login.jsp?rand=" + (Math.random()*10000).toString().substring(0, 4), null, "toolbar=no,menubar=yes,scrollbar=yes,resizable=yes,location=no,status=no,fullscreen=yes");
//	if(win != window && win != null)
//		toClose();
}

function showBillMsg()
{ 
	var table = document.getElementById('msgtable');
	table.rows[0].cells[0].innerHTML = window.status;
}

function cleanBillMsg()
{ 
	var table = document.getElementById('msgtable');
	table.rows[0].cells[0].innerHTML = '';		
}

/**
 * 用户注销
 */
function logout()
{
	ConfirmDialogComp.showDialog("${ml:trans('yer_main005')}", logoutFunc, null, null, null);
}

/**
 * 用户注销逻辑
 * @private
 */
function logoutFunc()
{
	var cmd = new CommonCommand('userLogOut');
	cmd.setAsync(true);
	cmd.setReturnFunc(returnFucForLogOut);
	cmd.exec();
	//processCommonLogic(null, "userLogOut", returnFucForLogOut, null, true, null, null);
   	function returnFucForLogOut(result, returnArgs, success)
   	{
   		if(success && result[0] == "success")
   		{
   			// 用户已经注销的标示,之所以设置此标示是因为下句函数调用中window.location.href的重新设置会调用到onbeforeunload,由于此时session
   			// 已经validate了,所以在lfwCoreController中会抛出"客户端传入了不正确的pageId:"
   			window.$logoutflag = true;
   			gotoLoginPageFuc();
   		}	
   		else
	   		showMessageDialog("${ml:trans('yer_main006')}");
   	}
}

function changeImage()
{
}

function doHelp()
{
}

/**
 * 根据功能节点号打开单据
 * @param funCode 功能节点号
 */
function openBill(funCode)
{
	if(funCode == null || funCode == "")
		return;
	
	// 输入不允许有中文
	if(funCode.lengthb() != funCode.length || !isDigital(funCode))	
	{
		showWarningDialog("${ml:trans('yer_main007')}");
		return;
	}	
	
	if(funCode.length >= 2)
	{
		var ds = getDataset("ds_" + funCode.substring(0, 2));
		if(ds != null)
		{
			for(var i = ds.getRowCount() - 1; i >= 0; i--)
			{
				if(ds.getRow(i).getCellValue(0) == funCode)
				{
					var dsRow = ds.getRow(i);
					ds.setRowSelected(ds.getRowIndex(dsRow));
					openNode(funCode, dsRow.getCellValue(ds.nameToIndex("linkURL")), null, {title:dsRow.getCellValue(ds.nameToIndex("label"))});
					break;
				}
			}		
		}
	}	
}

/**
 * openNode
 * @param name 打开tabitem的funcode（功能注册编码）
 * @param src iframe中的
 * @param funcode 打开页面的pageID
 * @param attrArr 自定义属性,例如{title:'',funProperty:''}
 */
function openNode(funcode, src, attrArr)
{ 
	//cleanBillMsg();
	if(src == null) 
		return;
	src = window.corePath + "/" + src;
	var title = null;
	if(attrArr != null && attrArr.title != null)
		title = attrArr.title;
	else
		title = funcode;//默认显示的是功能注册编码
	
	//默认以tab多页签打开
	var	openmode = getConfigAttribute("openNodeMode");
 	if(openmode == null || openmode == ''){
 		openmode = "0";
 	}
 	// 增加节点点击频率
	addFunNodeFrequency(funcode);
	
	switch(openmode){
		case "0":
			// 最多打开几个页签后提示(默认为5个)
			var openTabMax = null;
			if(attrArr != null && attrArr.openTabMax != null)
				openTabMax = attrArr.openTabMax;
			else
			 	openTabMax = 5;
			 	
			var tab = getComponent("pageTab"), items = tab.getTabItems(), count = items.length;
			for(var i = 0; i < count; i++)
			{
				// 如果已经打开过相同funcode的单据
				if(items[i].name == funcode)
				{
					// 说明已经打开过审批单据
					if(items[i].approveid){
		   				// 首先关闭审批单据
		   				tab.removeItemTab(i);
		   				// 打开新的单据
		   				var item = tab.createItem(funcode, title, true);
		   				tab.activeTab(tab.getItemIndex(item));
						item.getObjHtml().appendChild(getNodeFrame(funcode, src));
						delete(items[i].approveid);
						return;
					}
					else
					{
						tab.activeTab(tab.getItemIndex(items[i]));				
						return;
					}
				}
			}
			
			// 如果已经打开单据数超过设置的打开最大限度则进行提示
			if(count > openTabMax)
			{
				continueOpen.tab = tab;
				continueOpen.funcode = funcode;
				continueOpen.title = title;
				continueOpen.src = src;
				ConfirmDialogComp.showDialog("${ml:trans('yer_quickdesk001')}" + (count-1) + "${ml:trans('yer_quickdesk002')}", continueOpen, null, null, null);
			} 
			else 
			{
				var item1 = tab.createItem(funcode, title, true);
				// 如果打开的是审批单据则记录审批单据id
				if(attrArr != null && attrArr.approveid)
					item1.approveid = attrArr.approveid;
				tab.activeTab(tab.getItemIndex(item1));
				// 显示"加载图片"
				showLoadingDiv(item1.getObjHtml());
//				if(div != null)
//					showBillMsg();
				item1.getObjHtml().appendChild(getNodeFrame(funcode, src));
			}
			break;
		case "1":
			var tab = getComponent("pageTab");
			var items = tab.getTabItems();
			var count = items.length;
			if(count == 2){
				var iframe = items[1].getObjHtml().firstChild.firstChild;
				iframe.src = src;
				items[1].changeTitle(title);
				tab.activeTab(1);
			} else {
				var item1 = tab.createItem(funcode, title, true);
				tab.activeTab(1);
				var div = getNodeFrame(funcode,src);
				item1.getObjHtml().appendChild(div);
				//showBillMsg();
			}
			break;
		case "2":
			window.open( src, title, "toolbar=no,menubar=no,scrollbar=yes,resizable=yes,location=no,status=no,fullscreen=no");
			//showBillMsg();
			break;		
		default:
				break;
	}
};

function addFunNodeFrequency(funcode)
{ 
	var param = "funcode=" + funcode;
	var cmd = new CommonCommand('addFunNodeFrequency');
	cmd.setAsync(true);
	cmd.setParameter(param);
	cmd.exec();
   	//processCommonLogic(param, "addFunNodeFrequency", returnFuc, null, true, null, null);
   	//function returnFuc(result, returnArgs, exp){}
};

/**
 * 显示单据加载完毕前的loading图片
 */
function showLoadingDiv(parentHtml)
{
	if(parentHtml == null)
		return;
		
	if(window.$loadingDiv == null)
	{
		window.$loadingDiv = $ce("div");
		window.$loadingDiv.style.position = "absolute";
		window.$loadingDiv.style.display = "none";
		window.$loadingDiv.style.left = "0";
		window.$loadingDiv.style.top = "0";
		window.$loadingDiv.style.width = "100%";
		window.$loadingDiv.style.height = "100%";
		window.$loadingDiv.style.background = "#ffffff url('/lfw/frame/themes/webclassic/images/common/loading.gif') no-repeat center center";
	}
	parentHtml.appendChild(window.$loadingDiv);
	window.$loadingDiv.style.display = "block";
};

/**
 * @private
 */
function getNodeFrame(funcode,src)
{
	var div = $ce("DIV");
    div.id = "tab_item_div_" + funcode;
    div.style.top = "0px";
    div.style.left = "0px";                        
    div.style.height = "100%";
    div.style.width = "100%";
    
    var frame=document.createElement("iframe");
	frame.style.top = "0px";
    frame.style.left = "0px";                        
    frame.style.height = "100%";
    frame.style.width = "100%";
    frame.style.border = "0";
    if(IS_FF)
    	frame.style.background = "#FFFFFF";
    frame.frameBorder = "0";
	frame.src=src;
	div.appendChild(frame);
	return div;
};

/**
 * @private
 */
function continueOpen(tab,funcode,title,src)
{
	var tab = null;
	var funcode = null;
	var title = null;
	var src = null;
	if(continueOpen.tab != null)
		tab = continueOpen.tab;
	if(continueOpen.funcode != null)
		funcode = continueOpen.funcode;
	if(continueOpen.title != null)
		title = continueOpen.title;
	if(continueOpen.src != null)
		src = continueOpen.src;
		
	var item1 = tab.createItem(funcode, title, true);
	tab.activeTab(tab.getItemIndex(item1));
	var div = getNodeFrame(funcode,src);
	item1.getObjHtml().appendChild(div);
	delete(continueOpen.tab);
	delete(continueOpen.funcode);
	delete(continueOpen.title);
	delete(continueOpen.src);
};

function toClose()
{
	window.opener=null;
	window.open("","_self");
	window.close();
}

function quickLogin()
{
	var url = getCorePath() + "/mainpage/quicklogin.jsp?pageId=login.pagemeta&rand=" + (Math.random()*10000).toString().substring(0, 4);
	showDialog(url , "${ml:trans('yer_main012')}", "400", "350", "switch_dialog", true,true);		
}

/**
 *打开日志
 */
function openLog()
{
	var url = getCorePath() + "/mainpage/log.jsp?pageId=lfwmainmenu.log.pagemeta&rand=" + (Math.random()*10000).toString().substring(0, 4);
	showDialog(url , "${ml:trans('yer_main011')}", "700", "500", "log_dialog", true,true);
}

/**
 * 修改配置
 */
function modifyConfig()
{	
	var url = getCorePath() + "/prefs.jsp?pageId=bxprefs&rand=" + (Math.random()*10000).toString().substring(0, 4);
	showDialog(url , "${ml:trans('yer_main010')}", "340", "300", "modifyconfig", false, false); 	
};

/**
 * 根据key得到在cookie中相应的value
 */
function getCookieById(name)
{
	var allCookie = document.cookie;
	var pos = allCookie.indexOf("INVOCATION_KEY=");
	var value = null;
	if(pos != -1){
		var start = pos + 15;
		var end = allCookie.indexOf(";",start);
		if(end == -1) end = allCookie.length;
		value = allCookie.substring(start,end); 
	}
	var v = value.split("$");
	if(name == "accountCode") return v[0];
	else if(name == "usercode") return v[2];
	else if(name == "userType") return v[9];
	else if(name == "datasource") return v[7];
	else if(name == "accountName") return v[8];
};

function openBillNode(row)
{	
	if(openBillNode.mode == null)
		openBillNode.mode = getConfigAttribute("openNodeMode");
	openNode(row.getCellValue(0),row.getCellValue(2),openBillNode.mode,{title:row.getCellValue(1),openTabMax:5});
};

window.isFunNodeListHide = false;
function showHideList() {
	if(isFunNodeListHide == null || isFunNodeListHide == false){
		$('funnodeListTr').style.display = "none";
		$('funnodeShowImg').src = window.nodeImagePath + "/normal/hide-hover.gif";
		$('funnodeShowText').replaceChild(document.createTextNode("${ml:trans('yer_main008')}"),$('funnodeShowText').firstChild);
		isFunNodeListHide = true;
	}
	else{
		$('funnodeListTr').style.display = "block";
		$('funnodeShowImg').src = window.nodeImagePath + "/normal/hide-normal.gif";
		$('funnodeShowText').replaceChild(document.createTextNode("${ml:trans('yer_main009')}"),$('funnodeShowText').firstChild);
		isFunNodeListHide = false;
	}
}

function changeHideImage(img, state) {
	if(isFunNodeListHide == null || isFunNodeListHide == false){
		if(state == 0)
			img.src = window.nodeImagePath + "/normal/hide-normal.gif";
		else
			img.src = window.nodeImagePath + "/normal/hide-hover.gif";
	}
	else{
		if(state == 0)
			img.src = window.nodeImagePath + "/normal/more-normal.gif";
		else
			img.src = window.nodeImagePath + "/normal/more-hover.gif";
	}
}
